//
//  OperationManager.m
//  DoctorQA
//
//  Created by sally on 16/6/24.
//  Copyright © 2016年 talecare. All rights reserved.
//

#import "AppDelegate.h"
#import "ExpertDetailViewController.h"
#import "LoginViewController.h"
#import "OperationManager.h"

@implementation OperationManager

#pragma mark 配置segment
+ (HMSegmentedControl *)configSegment:(HMSegmentedControl *)_segmCtrl type:(SegmentedType)type {

  if (type == SegmentedType_News) { //消息页
    _segmCtrl.frame = CGRectMake((ScreenWidth - 180) / 2, 0, 180, NavHeight);
    _segmCtrl.titleTextAttributes = @{ NSForegroundColorAttributeName : GBlackColor,
                                       NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:GLargeFontSize] };
    _segmCtrl.selectedTitleTextAttributes = @{ NSForegroundColorAttributeName : GOrangeColor,
                                               NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:GLargeFontSize] };
    _segmCtrl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;

  } else {
    if (type == SegmentedType_OrderExpert) { //我约的专家页
      _segmCtrl.frame = CGRectMake((ScreenWidth - 210) / 2, 0, 210, NavHeight);

    } else if (type == SegmentedType_OrderMe) { //约我的人页
      _segmCtrl.frame = CGRectMake((ScreenWidth - 140) / 2, 0, 140, NavHeight);
    }
    _segmCtrl.titleTextAttributes = @{ NSForegroundColorAttributeName : GGrayWordColor1,
                                       NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:GNormalFontSize] };
    _segmCtrl.selectedTitleTextAttributes = @{ NSForegroundColorAttributeName : GOrangeColor,
                                               NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:GNormalFontSize] };
    _segmCtrl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
  }

  _segmCtrl.selectionIndicatorHeight = 2.0f; //设置颜色条粗细
  _segmCtrl.selectionIndicatorColor = GOrangeColor;
  _segmCtrl.segmentEdgeInset = UIEdgeInsetsMake(0, 6, 0, 6);
  _segmCtrl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
  _segmCtrl.tag = 1;
  _segmCtrl.selectedSegmentIndex = 0;
  return _segmCtrl;
}

#pragma mark 退回首页
+ (void)gotoTabOne {
  AppDelegate *app = (AppDelegate *) [UIApplication sharedApplication].delegate;
  UITabBarController *tabVc = (UITabBarController *) app.window.rootViewController;
  tabVc.selectedIndex = 0;
}

#pragma mark 退回我的管理
+ (void)gotoTabFour {
  AppDelegate *app = (AppDelegate *) [UIApplication sharedApplication].delegate;
  UITabBarController *tabVc = (UITabBarController *) app.window.rootViewController;
  tabVc.selectedIndex = 3;
}

#pragma mark 判断是否跳转登录页-返回是否登录
+ (BOOL)gotoLogin {
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];
  if (!userModel) {
    LoginViewController *vc = [[LoginViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.accountType = AccountType_Login;
    vc.isHiddenBack = NO;
    UIViewController *currVc = [[CommonMethod sharedCommonMethod] getCurrViewController];
    [currVc presentViewController:vc animated:YES completion:nil];
    return NO;
  }
  return YES;
}

#pragma mark 跳转专家详情页
+ (void)gotoExpertDetailVcWithId:(NSInteger)expertId expertName:(NSString *)expertName {
  ExpertDetailViewController *vc = [[ExpertDetailViewController alloc] init];
  vc.expertId = expertId;
  vc.expertName = expertName;
  vc.hidesBottomBarWhenPushed = YES;
  [[[CommonMethod sharedCommonMethod] getCurrNavController] pushViewController:vc animated:YES];
}

#pragma mark 跳转订单详情页
+ (OrderDetailViewController *)gotoOrderDetailVcWithType:(OrderDetailType)orderDetailType status:(NSInteger)status {
  OrderDetailViewController *vc = [[OrderDetailViewController alloc] init];
  vc.orderDetailType = orderDetailType;
  if (orderDetailType == OrderDetailType_Normal) { //普通用户
    vc.orderNormalStatus = (OrderNormalStatus) status;
  } else if (orderDetailType == OrderDetailType_Expert) { //专家
    vc.orderExpertStatus = (OrderExpertStatus) status;
  }
  [[[CommonMethod sharedCommonMethod] getCurrNavController] pushViewController:vc animated:YES];
  return vc;
}

#pragma mark 跳转话题详情页
+ (void)gotoThemeDetailVcWithModel:(ExpertDetailModel *)expertDetailModel topicModel:(TopicModel *)topicModel {
  ThemeDetailViewController *vc = [[ThemeDetailViewController alloc] init];
  vc.expertDetailModel = expertDetailModel;
  vc.topicModel = topicModel;
  [[[CommonMethod sharedCommonMethod] getCurrNavController] pushViewController:vc animated:YES];
}

#pragma mark 配置相册选择sheet
+ (void)configCameraActionSheet {
  UIActionSheet *actionSheet;
  UIViewController *currVc = [[CommonMethod sharedCommonMethod] getCurrViewController];
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    //支持拍照
    actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                              delegate:currVc
                                     cancelButtonTitle:@"取消"
                                destructiveButtonTitle:nil
                                     otherButtonTitles:@"拍照", @"从相册选择", nil];
  } else {
    //不支持拍照
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择"
                                              delegate:currVc
                                     cancelButtonTitle:@"取消"
                                destructiveButtonTitle:@"从相册选择"
                                     otherButtonTitles:nil, nil];
  }
  [actionSheet showInView:currVc.view];
}

#pragma mark 配置相册浏览器
+ (UIImagePickerController *)configImagePicker:(NSString *)selectStr {
  UIImagePickerControllerSourceType type;
  UIViewController *currVc = [[CommonMethod sharedCommonMethod] getCurrViewController];
  if ([selectStr isEqualToString:@"从相册选择"]) {
    type = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  } else if ([selectStr isEqualToString:@"拍照"]) {
    type = UIImagePickerControllerSourceTypeCamera;
  } else {
    return nil; //取消
  }

  UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  imagePicker.delegate = currVc;
  imagePicker.allowsEditing = NO;
  imagePicker.sourceType = type;
  [currVc presentViewController:imagePicker animated:YES completion:nil];
  return imagePicker;
}

#pragma mark 信鸽注册设备
+ (void)registXGDevice {
  NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:UD_LAST_DEVICE_TOKEN];
  if (deviceToken) {
    AccountInterface *interface = [[AccountInterface alloc] init];
    [interface registerDeviceWithDeviceId:deviceToken completion:^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    }
        error:^(NSError *error) {
          [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
        }];
  }
}

#pragma mark 信鸽注销设备
+ (void)deleteXGDevice {
  NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:UD_LAST_DEVICE_TOKEN];
  if (deviceToken) {
    AccountInterface *interface = [[AccountInterface alloc] init];
    [interface deleteDeviceWithDeviceId:deviceToken completion:^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    }
        error:^(NSError *error) {
          [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
        }];
  }
}

#pragma mark 友盟分享内容设置
+ (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType title:(NSString *)title desc:(NSString *)desc imageUrl:(NSString *)imageUrl webUrl:(NSString *)webUrl {
  //创建分享消息对象
  UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

  //创建网页内容对象
  UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:desc thumImage:imageUrl];
  //设置网页地址
  shareObject.webpageUrl = webUrl;

  //分享消息对象设置分享内容对象
  messageObject.shareObject = shareObject;

  //调用分享接口
  [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
    if (error) {
      UMSocialLogInfo(@"************Share fail with error %@*********", error);
    } else {
      if ([data isKindOfClass:[UMSocialShareResponse class]]) {
        UMSocialShareResponse *resp = data;
        //分享结果消息
        UMSocialLogInfo(@"response message is %@", resp.message);
        //第三方原始返回的数据
        UMSocialLogInfo(@"response originalResponse data is %@", resp.originalResponse);

      } else {
        UMSocialLogInfo(@"response data is %@", data);
      }
    }
  }];
}

#pragma mark 验证金额的正则
+ (BOOL)validateMoney:(NSString *)money {
  NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
  NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
  return [phoneTest evaluateWithObject:money];
}

#pragma mark 验证电话的正则
+ (BOOL)isMobileNumber:(NSString *)mobileNum {

  /**
   * 手机号码:
   * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
   * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
   * 联通号段: 130,131,132,155,156,185,186,145,176,1709
   * 电信号段: 133,153,180,181,189,177,1700
   */
  NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
  /**
   * 中国移动：China Mobile
   * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
   */
  NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
  /**
   * 中国联通：China Unicom
   * 130,131,132,155,156,185,186,145,176,1709
   */
  NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
  /**
   * 中国电信：China Telecom
   * 133,153,180,181,189,177,1700
   */
  NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";

  /**
   25         * 大陆地区固话及小灵通
   26         * 区号：010,020,021,022,023,024,025,027,028,029
   27         * 号码：七位或八位
   28         */
  //   NSString * PHS = @"^(0[0-9]{2})\\d{8}$|^(0[0-9]{3}(\\d{7,8}))$";

  NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
  NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
  NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

  if (([regextestmobile evaluateWithObject:mobileNum] == YES) || ([regextestcm evaluateWithObject:mobileNum] == YES) || ([regextestct evaluateWithObject:mobileNum] == YES) || ([regextestcu evaluateWithObject:mobileNum] == YES)) {
    return YES;
  } else {
    return NO;
  }
}

+ (BOOL)validateEmail:(NSString *)email {
  NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  return [emailTest evaluateWithObject:email];
}

@end
